package com.mysher.devlib.camera2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CameraIdUtils {

    private CameraIdUtils() {
    }

    public static int getIndexOfCamera(int[] vidAndPids, List<String> CameraList) {
        int index = -1;
        List<String> list = CameraList;
        if (CameraList == null || CameraList.isEmpty()) {
            list = getCameraList();
        }
        for (int i = 0; i < list.size(); i++) {
            String camera = (String) list.get(i);
            int cvid = Integer.parseInt(camera.substring(0, 4), 16);
            int cpid = Integer.parseInt(camera.substring(5, 9), 16);
            for (int j = 0; j < vidAndPids.length; j += 2) {
                if (vidAndPids[j] == cvid && vidAndPids[j + 1] == cpid) {
                    index = i;
                    break;
                }
            }
            if (index != -1) {
                break;
            }
        }
        return index;
    }

    public static int getIndexOfCamera(int vid, int pid, List<String> CameraList) {
        int index = -1;
        List<String> list = CameraList;
        if (CameraList == null || CameraList.isEmpty()) {
            list = getCameraList();
        }

        for (int i = 0; i < list.size(); ++i) {
            String camera = (String) list.get(i);
            int cvid = Integer.parseInt(camera.substring(0, 4), 16);
            int cpid = Integer.parseInt(camera.substring(5, 9), 16);
            if (vid == cvid && pid == cpid) {
                index = i;
                break;
            }
        }
        return index;
    }

    public static List<String> getCameraList() {
        List<String> list = new ArrayList();
        int j = 0;

        for (int i = 0; i < 100; ++i) {
            String res = shellExec("cat /sys/class/video4linux/video" + i + "/device/modalias");
            if (res.equals("") && j++ > 10) {
                break;
            }
            if (res.startsWith("usb:v")) {
                String camera = res.substring(5, 9) + ":" + res.substring(10, 14);
                if (!list.contains(camera)) {
                    list.add(camera);
                }
            }
        }

        return list;
    }

    public static String shellExec(String command) {
        String result = "";
        Runtime mRuntime = Runtime.getRuntime();

        try {
            Process mProcess = mRuntime.exec(command);
            BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
            StringBuffer mRespBuff = new StringBuffer();
            char[] buff = new char[1024];
            boolean var7 = false;

            int ch;
            while ((ch = mReader.read(buff)) != -1) {
                mRespBuff.append(buff, 0, ch);
            }

            mReader.close();
            result = mRespBuff.toString();
        } catch (IOException var8) {
            var8.printStackTrace();
            result = "Execution error:" + var8.toString();
        }

        return result;
    }
}
